package com.hsxc.cdz.monitor.bean;

import java.io.Serializable;

import com.hsxc.cdz.base.util.BytesUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

/**
 * @author 作者 : 陈钊
 * @version 创建时间：2016年9月20日 下午8:28:08 
 * 类说明： 协议V1.21及V1.3.3的报文
 * 客户端和服务端报文头相同，共用一个实体类
 */
public class MessageNew implements Serializable {
	private static final long serialVersionUID = 1L;

	private String isOpen; // 启动字符
	private int contentLength; // 报文长度
	private String isRegister; // 是否注册
	private String response; // 响应码
	private String operator; // 运营商
	private String address; // 设备地址
	private String frame; // 帧类型
	private String lineNum; // 流水号
	private String protocolVersion; // 协议版本号
	private int pileType;// 充电桩类型
	private byte[] content; // 消息体

	public int getPileType() {
		return pileType;
	}

	public void setPileType(int pileType) {
		this.pileType = pileType;
	}

	public String getProtocolVersion() {
		return protocolVersion;
	}

	public void setProtocolVersion(String protocolVersion) {
		this.protocolVersion = protocolVersion;
	}

	public String getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(String isOpen) {
		this.isOpen = isOpen;
	}

	public int getContentLength() {
		return contentLength;
	}

	public void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}

	public String getIsRegister() {
		return isRegister;
	}

	public void setIsRegister(String isRegister) {
		this.isRegister = isRegister;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFrame() {
		return frame;
	}

	public void setFrame(String frame) {
		this.frame = frame;
	}

	public String getLineNum() {
		return lineNum;
	}

	public void setLineNum(String lineNum) {
		this.lineNum = lineNum;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	// 组装服务器下发的报文
	public byte[] getByteFromMessage() {

		ByteBuf bytes = Unpooled.buffer(29 + this.contentLength);
		byte[] result;
		byte[] temp;
		temp = BytesUtil.strToBinBytes(this.getIsOpen(), 2);
		bytes.writeBytes(temp);
		temp = BytesUtil.intToBytesDesc(this.getContentLength(), 2);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getIsRegister(), 1);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getResponse(), 3);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBcdBytes(this.getOperator(), 2);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBcdBytes(this.getAddress(), 8);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getFrame(), 1);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBcdBytes(this.getLineNum(), 7);
		bytes.writeBytes(temp);
		temp = BytesUtil.strToBinBytes(this.getProtocolVersion(), 2);
		bytes.writeBytes(temp);
		temp = BytesUtil.intToBytesSeq(this.getPileType(), 1);
		bytes.writeBytes(temp);

		temp = this.getContent();
		if (temp != null) {
			bytes.writeBytes(temp);
		}

		result = new byte[bytes.readableBytes()];
		bytes.readBytes(result);
		ReferenceCountUtil.release(bytes);
		return result;
	}

	// 将充电桩上传的报文分解到实体类
	public void getMessageFromByte(byte[] result1) {
		if (result1 == null) {
			return;
		}
		byte[] temp;
		temp = BytesUtil.getByte(0, 2, result1);
		this.setIsOpen(BytesUtil.binBytesToStr(temp));

		temp = BytesUtil.getByte(2, 2, result1);
		this.setContentLength(BytesUtil.bytesToIntDesc(temp));

		temp = BytesUtil.getByte(4, 1, result1);
		this.setIsRegister(BytesUtil.binBytesToStr(temp));

		temp = BytesUtil.getByte(5, 3, result1);
		this.setResponse(BytesUtil.binBytesToStr(temp));

		temp = BytesUtil.getByte(8, 2, result1);
		this.setOperator(BytesUtil.bcdBytesToStr(temp));

		temp = BytesUtil.getByte(10, 8, result1);
		this.setAddress(BytesUtil.bcdBytesToStr(temp));

		temp = BytesUtil.getByte(18, 1, result1);
		this.setFrame(BytesUtil.binBytesToStr(temp));

		temp = BytesUtil.getByte(19, 7, result1);
		this.setLineNum(BytesUtil.bcdBytesToStr(temp));

		temp = BytesUtil.getByte(26, 2, result1);
		this.setProtocolVersion(BytesUtil.binBytesToStr(temp));

		temp = BytesUtil.getByte(28, 1, result1);
		this.setPileType(BytesUtil.bytesToIntSeq(temp));
	}

	@Override
	public String toString() {
		return "message [isOpen=" + isOpen + ", contentLength=" + contentLength + ", isRegister=" + isRegister
				+ ", response=" + response + ", operator=" + operator + ", address=" + address + ", frame=" + frame
				+ ", lineNum=" + lineNum + ", protocolVersion" + protocolVersion + ",pileType" + pileType + "]";
	}
}
